package com.richard.base.net.common

import android.app.Application
import androidx.annotation.Keep
import androidx.annotation.UiThread
import com.ks.component.net.NetConstants
import com.richard.base.BaseApplication
import com.richard.base.BuildConfig
import retrofit2.http.Headers
import java.io.InputStream
import java.util.ArrayList

/**
 * 初始化 网络组件
 */
@Keep
object NetComponent {

    private var config: NetConfig? = null
    fun init() {
        checkConfig()
    }

    internal fun getHeader(): Map<String, String> {
        return checkConfig().getHeaderMap()
    }

    internal val isNeedSSL: Boolean by lazy { checkConfig().isNeedSSL() }

    internal val baseUrl: String by lazy { checkConfig().baseUrl() }

    val application: Application by lazy { checkConfig().application() }

    fun getAssetsPems(): MutableList<InputStream> {
        return checkConfig().getAssetsPems()
    }

    interface NetConfig {

        fun baseUrl(): String

        fun isNeedSSL(): Boolean

        fun getHeaderMap(): Map<String, String>

        fun application(): Application

        fun getAssetsPems(): MutableList<InputStream>
    }




    fun checkConfig(): NetConfig {
        if (config == null) {
            config = object : NetConfig {
                override fun baseUrl(): String {
                    return BuildConfig.BASE_URL
                }

                override fun isNeedSSL(): Boolean {
                    return false
                }

                override fun getHeaderMap(): Map<String, String> {
                    return HashMap<String, String>().apply {
                        put(NetConstants.CONTENT_TYPE, NetConstants.HeaderContentTypeLong)
                        put(NetConstants.TOKEN, NetConstants.HeaderToken)
                        put(NetConstants.DEVICE, "android")
                        put(NetConstants.ACCEPT_VERSION, "v1")
                    }
                }

                override fun application(): Application {
                    return BaseApplication.instance
                }

                override fun getAssetsPems(): MutableList<InputStream> {
                    val certificates: MutableList<InputStream> = ArrayList()
                    try {
                        //old *.kaishustory
                        val kaishu = BaseApplication.instance.assets.open("kaishustorycom.pem")
                        certificates.add(kaishu)
                        //new *.kaishustory
                        val newkaishu = BaseApplication.instance.assets.open("new_kaishustorycom.pem")
                        certificates.add(newkaishu)
                        // charles
                        val charles = BaseApplication.instance.assets.open("charles-ssl-proxying-certificate.pem")
                        certificates.add(charles)
                        // cdn
                        val hezi = BaseApplication.instance.assets.open("cdn_kaishuhe_com.pem")
                        certificates.add(hezi)
                        //old  ksjgs(短域名)
                        val ksjgs = BaseApplication.instance.assets.open("ksjgs_com.pem")
                        certificates.add(ksjgs)
                        //new ksjgs(短域名)
                        val newksjgs = BaseApplication.instance.assets.open("new_ksjgs_com.pem")
                        certificates.add(newksjgs)

                    } catch (e: Exception) {
                    }
                    return certificates
                }

            }
        }

        return config!!
    }
}